﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class QuanDAO
    {
        #region 1.Retrieving
        public static List<QuanDTO> SelectQuanAll()
        {
            List<QuanDTO> listQuan = new List<QuanDTO>();

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectQuansAll");
            foreach (DataRow dr in dt.Rows)
            {
                QuanDTO quanDTO = new QuanDTO();
                quanDTO.TenQuan = dr["TenQuan"].ToString();
                quanDTO.MaQuan = dr["MaQuan"].ToString();
                listQuan.Add(quanDTO);
            }


            return listQuan;
        }
        public static List<QuanDTO> SellectQuanByIndex(string maQuan)
        {
            List<QuanDTO> listQuanDTO = new List<QuanDTO>();

            List<SqlParameter> listSql = new List<SqlParameter>();
            listSql.Add(new SqlParameter("@MaQuan", maQuan));
            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectQuansByAndMaQuan", listSql);
            foreach (DataRow dr in dt.Rows)
            {
                QuanDTO quanDTO = new QuanDTO();
                quanDTO.MaQuan = dr["MaQuan"].ToString();
                quanDTO.TenQuan = dr["TenQuan"].ToString();

                listQuanDTO.Add(quanDTO);
            }

            return listQuanDTO;
        }
        public static bool CheckMaQuanExits(string maQuan)
        {
            bool isExist = false;
            List<QuanDTO> listQuanDTO = new List<QuanDTO>();

            List<SqlParameter> listSql = new List<SqlParameter>();
            listSql.Add(new SqlParameter("@MaQuan", maQuan));
            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectQuansByAndMaQuan", listSql);
            foreach (DataRow dr in dt.Rows)
            {
                QuanDTO quanDTO = new QuanDTO();
                quanDTO.MaQuan = dr["MaQuan"].ToString();
                quanDTO.TenQuan = dr["TenQuan"].ToString();

                listQuanDTO.Add(quanDTO);
            }

            if (listQuanDTO.Count == 1)
            {
                isExist = true;
            }

            return isExist;
        }
        #endregion
        #region 2.Updating
        public static bool UpdateQuan(QuanDTO quan)
        {
            bool isSucessful = false;

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@TenQuan", quan.TenQuan));
            listSqlParam.Add(new SqlParameter("@MaQuan", quan.MaQuan));
            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_UpdateQuan", listSqlParam);
            if (n == -1)
            {
                isSucessful = true;
            }

            return isSucessful;
        }
        #endregion
        #region 3.Inserting
        public static bool InsertQuan(QuanDTO quan)
        {
            bool isSucessful = false;

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@TenQuan", quan.TenQuan));
            listSqlParam.Add(new SqlParameter("@MaQuan", quan.MaQuan));
            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_InsertQuan", listSqlParam);
            if (n == -1)
            {
                isSucessful = true;
            }

            return isSucessful;
        }
        #endregion
        #region 4.Deleting
        public static bool DeleteQuan(string maQuan)
        {
            bool isSucessful = false;

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaQuan", maQuan));

            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_DeleteQuan", listSqlParam);
            if (n == -1)
            {
                isSucessful = true;
            }

            return isSucessful;
        }
        #endregion

    }
}
